﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class ImagesData :DataBase
    {
        public void SetNewsImageBind(int newsId, string imageName)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@NewsID", newsId),
                                     new SqlParameter("@Name", imageName),
                                     new SqlParameter("@Url", ConfigurationManager.AppSettings["NewsImageUrl"])
                                 };
            ExecuteNonQuery("SetNewsImageBind", parameters);
        }

        public List<ImageInNews> GetNewsImagesUrl(int? newsId = null)
        {
            var dataTable = ExecuteDataTable("GetNewsImagesUrl",
                                             new List<SqlParameter> {new SqlParameter("@NewsID", newsId)
                                             });
            if(dataTable.Rows.Count > 0)
            {
                var imagesUrlList = new List<ImageInNews>();
                foreach (DataRow row in dataTable.Rows)
                {
                    imagesUrlList.Add(new ImageInNews
                                          {
                                              Id = Convert.ToInt32(row["ID"]),
                                              Name = Convert.ToString(row["Name"]),
                                              NewsId = Convert.ToInt32(row["NewsID"]),
                                              Url = Convert.ToString(row["Url"])
                                          });
                }
                return imagesUrlList;
            }
            return null;
        }

        public List<ImageInNews> GetNewsImages(int? newsId = null, int? id = null)
        {
            var dataTable = ExecuteDataTable("GetNewsImages",
                                             new List<SqlParameter> 
                                             {
                                                new SqlParameter("@NewsID", newsId),
                                                new SqlParameter("@ID",id)
                                             });
            if (dataTable.Rows.Count > 0)
            {
                var imagesList = new List<ImageInNews>();
                foreach (DataRow row in dataTable.Rows)
                {
                    imagesList.Add(new ImageInNews
                                       {
                                           Id = Convert.ToInt32(row["ID"]),
                                           Name = Convert.ToString(row["Name"]),
                                           NewsId = Convert.ToInt32(row["NewsID"]),
                                           Url = Convert.ToString(row["Url"])
                                       });
                }
                return imagesList;
            }
            return new List<ImageInNews>();
        }
        public void DeleteNewsImage(int id)
        {
            ExecuteNonQuery("DeleteNewsImage", new List<SqlParameter> {new SqlParameter("@ID", id)});
        }

        public void SetUserImageBind(int userId, string imageName)
        {

            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@UserID", userId),
                                     new SqlParameter("@Name", imageName),
                                     new SqlParameter("@Url", ConfigurationManager.AppSettings["UsersImageUrl"])
                                 };
            ExecuteNonQuery("SetUserImageBind", parameters);
        }

        public List<String> GetUserImagesUrl(int? userId = null)
        {
            var dataTable = ExecuteDataTable("GetUserImagesUrl",
                                             new List<SqlParameter> {new SqlParameter("@UserID", userId)
                                             });
            if (dataTable.Rows.Count > 0)
            {
                var imagesUrlList = new List<String>();
                foreach (DataRow row in dataTable.Rows)
                {
                    imagesUrlList.Add(Convert.ToString(row["Url"]));
                }
                return imagesUrlList;
            }
            return null;
        }

        public List<UserImage> GetUserImages(int? userId = null, int? id = null)
        {
            var dataTable = ExecuteDataTable("GetUserImages",
                                             new List<SqlParameter> 
                                             {
                                                new SqlParameter("@UserID", userId),
                                                new SqlParameter("@ID",id)
                                             });
            if (dataTable.Rows.Count > 0)
            {
                var imagesList = new List<UserImage>();
                foreach (DataRow row in dataTable.Rows)
                {
                    imagesList.Add(new UserImage
                    {
                        Id = Convert.ToInt32(row["ID"]),
                        Name = Convert.ToString(row["Name"]),
                        UserId = Convert.ToInt32(row["UserID"]),
                        Url = Convert.ToString(row["Url"])
                    });
                }
                return imagesList;
            }
            return new List<UserImage>();
        }
        public void DeleteUserImage(int id)
        {
            ExecuteNonQuery("DeleteUserImage", new List<SqlParameter> { new SqlParameter("@ID", id) });
        }
    }
}